const pageConf = require('../config/config').page_conf,
  indexModel = require('../models/index');

const extention = require('../lib/extentions');

class IndexController {
  //主页渲染
  async index(ctx, next) {

    const data = await indexModel.getXiaomiDatas({
      swiper: true,
      phone: true,
      field: true
    });

    const swiperData = data.swiper_data,
      phoneData = data.phone_data,
      fieldData = data.field_data;

    await ctx.render('index', {
      pageConf: pageConf.index,
      swiperData,
      phoneData,
      fieldData
    })
  }

  //搜索的list
  async list(ctx, next) {

    const keyword = ctx.params.keyword;

    const data = await indexModel.getXiaomiDatas({
      swiper: false,
      phone: true,
      field: true
    });

    const phoneData = data.phone_data,
      fieldData = data.field_data,
      searchData = !keyword ? phoneData : extention.filterData(phoneData, keyword);

    await ctx.render('list', {
      pageConf: pageConf.list,
      phoneData,
      fieldData,
      searchData
    })
  }

  //详情页
  async detail(ctx, next) {
    const id = ctx.params.id;

    const data = await indexModel.getXiaomiDatas({
      swiper: false,
      phone: true,
      field: true
    });

    const phoneData = data.phone_data,
      fieldData = data.field_data,
      phoneDetail = phoneData.filter(item => item.id === id )[0];

    await ctx.render('detail', {
      pageConf: pageConf.detail,
      phoneData,
      fieldData,
      phoneDetail
    })
  }

  //404
  async my404 (ctx, next) {

    
    const data = await indexModel.getXiaomiDatas({
      swiper: false,
      phone: true,
      field: true
    });

    const phoneData = data.phone_data,
      fieldData = data.field_data;

    await ctx.render('404', {
      title: '404',
      pageConf: pageConf.my404,
      phoneData,
      fieldData
    })
  }
}

module.exports = new IndexController();